home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_01 / ed_157 / store_param.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-18  |  8.8 KB  |  353 lines

  1. /*
  2.  * Copyright (C) 1992 by Rush Record
  3.  * Copyright (C) 1993 by Charles Sandmann (sandmann@clio.rice.edu)
  4.  * 
  5.  * This file is part of ED.
  6.  * 
  7.  * ED is free software; you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation.
  9.  * 
  10.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  11.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  12.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  13.  * 
  14.  * You should have received a copy of the GNU General Public License along with ED
  15.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  16.  * Mass Ave, Cambridge, MA 02139, USA.
  17.  */
  18. #include "opsys.h"
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #include "memory.h"
  24. #include "rec.h"
  25. #include "window.h"
  26. #include "ed_dec.h"
  27. #include "cmd_enum.h"
  28. #include "cmd_dec.h"
  29.  
  30. extern Char *filename();
  31.  
  32. #define NUM_FILEENTRIES 19    /* number of a=b lines (other than ndefined=) */
  33.  
  34. /******************************************************************************\
  35. |Routine: fixup_delim
  36. |Callby: store_param
  37. |Purpose: Converts control characters to printable form for the setup file.
  38. |Arguments:
  39. |    buf is the buffer that gets the data in printable form.
  40. |    l is the length of the original data.
  41. |    d is the buffer that has the original data, including control characters.
  42. \******************************************************************************/
  43. void fixup_delim(buf,l,d)
  44. register Char *buf,*d;
  45. register Int l;
  46. {
  47.     register Int i,j;
  48.  
  49.     for(i = j = 0;i < l;i++)
  50.     {
  51.         if(d[i] < 33)
  52.         {
  53.             buf[j++] = 0x1d;
  54.             sprintf(buf + j,"%03d",((int)d[i]) & 0xff);
  55.             j += strlen(buf + j);
  56.         }
  57.         else
  58.             buf[j++] = d[i];
  59.     }
  60.     buf[j] = '\0';
  61. }
  62.  
  63. /******************************************************************************\
  64. |Routine: store_param
  65. |Callby: command
  66. |Purpose: Stores editor settings in the user's startup file.
  67. |Arguments:
  68. |    choice is the number of the setting to store, or -1 to store all settings.
  69. \******************************************************************************/
  70. void store_param(choice)
  71. Int choice;
  72. {
  73.     FILE *ifp,*ofp;
  74.     Int i,ntab;
  75.     Char buf[512],tmpfile[512],*p;
  76.     Char found[NUM_FILEENTRIES];
  77.     static Char *keyword[NUM_FILEENTRIES] =
  78.     {
  79.         "shell=",
  80.         "wrap=",
  81.         "section=",
  82.         "word=",
  83.         "page=",
  84.         "paragraph=",
  85.         "search=",
  86.         "stable=",
  87.         "close=",
  88.         "parens=",
  89.         "defext=",
  90.         "auto=",
  91.         "cfriendly=",
  92.         "boxmode=",
  93.         "wraparound=",
  94.         "casechange=",
  95.         "overstrike=",
  96.         "grepmode=",
  97.         "wildcard="
  98.     };
  99.     static Char *deflt[NUM_FILEENTRIES] = 
  100.     {
  101.         "/bin/csh",
  102.         "80",
  103.         "18",
  104.         " \t,.+=-*/[]{}()\n\v\f\r",
  105.         "\f",
  106.         "\r\r",
  107.         "gbnnn",
  108.         "~/categories.st",
  109.         "y",
  110.         "(){}[]",
  111.         "c h f",
  112.         "n",
  113.         "n",
  114.         "n",
  115.         "n",
  116.         "Opposite",
  117.         "n",
  118.         "verbose",
  119.         "?"
  120.     };
  121.  
  122.     if(!(ifp = fopen(KEYFILE,"r")))
  123.     {
  124.         slip_message("Unable to open the original setup file.");
  125.         wait_message();
  126.         return;
  127.     }
  128.     strcpy(tmpfile,KEYFILE);
  129.     *(filename(tmpfile)) = '\0';
  130.     strcat(tmpfile,"_key_.tmp");
  131. #ifdef VMS
  132.     if(!(ofp = fopen(tmpfile,"w","ctx=rec","rfm=var","rat=cr","shr=nil")))
  133. #else
  134.     if(!(ofp = fopen(tmpfile,"w")))
  135. #endif
  136.     {
  137.         fclose(ifp);
  138.         slip_message("Unable to write to the setup file.");
  139.         wait_message();
  140.         return;
  141.     }
  142. /* transfer the journal file name */
  143.     fgets(buf,sizeof(buf),ifp);
  144.     fputs(buf,ofp);
  145. /* clear all found fields */
  146.     memset(found,0,sizeof(found));
  147. /* read the setup file and process changes */
  148.     while(fgets(buf,sizeof(buf),ifp))
  149.     {
  150.         if((p = strchr(buf,'\n')))
  151.             *p = '\0';
  152.         if((p = strstr(buf,keyword[0])))    /* the shell name */
  153.         {
  154.             found[0] = 1;
  155.             p += strlen(keyword[0]);
  156.             if(choice == (int)PAR_SHELL || choice < 0)
  157.                 strcpy(p,USERSHELL);
  158.             fprintf(ofp,"%s\n",buf);
  159.         }
  160.         else if((p = strstr(buf,"tabs=")))    /* tabs */
  161.         {
  162.             p += strlen("tabs=");
  163.             my_sscanf(p,"%d",&ntab);
  164.             fprintf(ofp,"%s\n",buf);
  165.             my_fscanf(ifp,"curtab=%d\n",&i);
  166.             if(choice == (int)PAR_TABS || choice < 0)
  167.                 i = CUR_TAB_SETUP;
  168.             fprintf(ofp,"curtab=%d\n",i);
  169.             for(i = 0;i < ntab << 1;i++)
  170.             {
  171.                 fgets(buf,sizeof(buf),ifp);
  172.                 fputs(buf,ofp);
  173.             }
  174.         }
  175.         else if((p = strstr(buf,keyword[1])))    /* word wrap margin */
  176.         {
  177.             found[1] = 1;
  178.             p += strlen(keyword[1]);
  179.             my_sscanf(p,"%d",&i);
  180.             if(choice == (int)PAR_WRAP || choice < 0)
  181.                 i = WRAP_MARGIN;
  182.             fprintf(ofp,"wrap=%d\n",i);
  183.         }
  184.         else if((p = strstr(buf,keyword[2])))    /* section lines */
  185.         {
  186.             found[2] = 1;
  187.             p += strlen(keyword[2]);
  188.             my_sscanf(p,"%d",&i);
  189.             if(choice == (int)PAR_SECTION || choice < 0)
  190.                 i = SECTION_LINES;
  191.             fprintf(ofp,"section=%d\n",i);
  192.         }
  193.         else if((p = strstr(buf,keyword[3])))    /* word delimiters */
  194.         {
  195.             found[3] = 1;
  196.             p += strlen(keyword[3]);
  197.             if(choice == (int)PAR_WORD || choice < 0)
  198.                 fixup_delim(p,NWORD_DELIMITERS,WORD_DELIMITERS);
  199.             fprintf(ofp,"%s\n",buf);
  200.         }
  201.         else if((p = strstr(buf,keyword[4])))    /* page break */
  202.         {
  203.             found[4] = 1;
  204.             p += strlen(keyword[4]);
  205.             if(choice == (int)PAR_PAGE || choice < 0)
  206.                 fixup_delim(p,PAGE_BREAK_LENGTH,PAGE_BREAK);
  207.             fprintf(ofp,"%s\n",buf);
  208.         }
  209.         else if((p = strstr(buf,keyword[5])))    /* word paragraph delimiter */
  210.         {
  211.             found[5] = 1;
  212.             p += strlen(keyword[5]);
  213.             if(choice == (int)PAR_PARAGRAPH || choice < 0)
  214.                 fixup_delim(p,PARAGRAPH_BREAK_LENGTH,PARAGRAPH_BREAK);
  215.             fprintf(ofp,"%s\n",buf);
  216.         }
  217.         else if((p = strstr(buf,keyword[6])))    /* search mode */
  218.         {
  219.             found[6] = 1;
  220.             p += strlen(keyword[6]);
  221.             if(choice == (int)PAR_SEARCH || choice < 0)
  222.                 memcpy(p,SEARCH_FLAGS,NUM_SMODES >> 1);
  223.             *(p + (NUM_SMODES >> 1)) = '\0';
  224.             fprintf(ofp,"%s\n",buf);
  225.         }
  226.         else if((p = strstr(buf,keyword[7])))    /* search table */
  227.         {
  228.             found[7] = 1;
  229.             p += strlen(keyword[7]);
  230.             if(choice == (int)PAR_STABLE || choice < 0)
  231.                 strcpy(p,SEARCH_TABLE);
  232.             fprintf(ofp,"%s\n",buf);
  233.         }
  234.         else if((p = strstr(buf,keyword[8])))    /* paren matching status */
  235.         {
  236.             found[8] = 1;
  237.             p += strlen(keyword[8]);
  238.             if(choice == (int)PAR_CLOSE_PARENS || choice < 0)
  239.                 *p = (CLOSE_PARENS)? 'y' : 'n';
  240.             fprintf(ofp,"%s\n",buf);
  241.         }
  242.         else if((p = strstr(buf,keyword[9])))    /* paren pairs */
  243.         {
  244.             found[9] = 1;
  245.             p += strlen(keyword[9]);
  246.             if(choice == (int)PAR_PARENS || choice < 0)
  247.                 strcpy(p,PAREN_STRING);
  248.             fprintf(ofp,"%s\n",buf);
  249.         }
  250.         else if((p = strstr(buf,keyword[10])))    /* the defext string */
  251.         {
  252.             found[10] = 1;
  253.             p += strlen(keyword[10]);
  254.             if(choice == (int)PAR_DEFAULT || choice < 0)
  255.                 strcpy(p,DEFAULT_EXT);
  256.             fprintf(ofp,"%s\n",buf);
  257.         }
  258.         else if((p = strstr(buf,keyword[11])))    /* auto tab status */
  259.         {
  260.             found[11] = 1;
  261.             p += strlen(keyword[11]);
  262.             if(choice == (int)PAR_AUTO_TABS || choice < 0)
  263.                 *p = (TAB_AUTO)? 'y' : 'n';
  264.             fprintf(ofp,"%s\n",buf);
  265.         }
  266.         else if((p = strstr(buf,keyword[12])))    /* cfriendly status */
  267.         {
  268.             found[12] = 1;
  269.             p += strlen(keyword[12]);
  270.             if(choice == (int)PAR_CFRIENDLY || choice < 0)
  271.                 *p = (CFRIENDLY)? 'y' : 'n';
  272.             fprintf(ofp,"%s\n",buf);
  273.         }
  274.         else if((p = strstr(buf,keyword[13])))    /* box mode */
  275.         {
  276.             found[13] = 1;
  277.             p += strlen(keyword[13]);
  278.             if(choice == (int)PAR_BOXCUT || choice < 0)
  279.                 *p = (BOXCUT)? 'y' : 'n';
  280.             fprintf(ofp,"%s\n",buf);
  281.         }
  282.         else if((p = strstr(buf,keyword[14])))    /* wraparound flag */
  283.         {
  284.             found[14] = 1;
  285.             p += strlen(keyword[14]);
  286.             if(choice == (int)PAR_AUTOWRAP || choice < 0)
  287.                 *p = (AUTOWRAP)? 'y' : 'n';
  288.             fprintf(ofp,"%s\n",buf);
  289.         }
  290.         else if((p = strstr(buf,keyword[15])))    /* case change */
  291.         {
  292.             found[15] = 1;
  293.             p += strlen(keyword[15]);
  294.             if(choice == (int)PAR_CASE || choice < 0)
  295.                 strcpy(p,caseprompts[CASECHANGE]);
  296.             fprintf(ofp,"%s\n",buf);
  297.         }
  298.         else if((p = strstr(buf,keyword[16])))    /* overstrike flag */
  299.         {
  300.             found[16] = 1;
  301.             p += strlen(keyword[16]);
  302.             if(choice == (int)PAR_OVERSTRIKE || choice < 0)
  303.                 *p = (OVERSTRIKE)? 'y' : 'n';
  304.             fprintf(ofp,"%s\n",buf);
  305.         }
  306.         else if((p = strstr(buf,keyword[17])))    /* grep mode */
  307.         {
  308.             found[17] = 1;
  309.             p += strlen(keyword[17]);
  310.             if(choice == (int)PAR_GREPMODE || choice < 0)
  311.                 strcpy(p,(GREPMODE)? "silent" : "verbose");
  312.             fprintf(ofp,"%s\n",buf);
  313.         }
  314.         else if((p = strstr(buf,keyword[18])))    /* grep mode */
  315.         {
  316.             found[18] = 1;
  317.             p += strlen(keyword[18]);
  318.             if(choice == (int)PAR_WILDCARD || choice < 0)
  319.             {
  320.                 *p++ = WILDCARD;
  321.                 *p = '\0';
  322.             }
  323.             fprintf(ofp,"%s\n",buf);
  324.         }
  325.         else if((p = strstr(buf,"ndefined=")))    /* key definitions, must be last thing in file */
  326.         {
  327. /* check for unmentioned things and make setup file up-to-date */
  328.             for(i = 0;i < NUM_FILEENTRIES;i++)
  329.                 if(!found[i])
  330.                     fprintf(ofp,"%s%s\n",keyword[i],deflt[i]);
  331.             if(choice != (int)PAR_KEYS && choice >= 0)
  332.             {
  333.                 fprintf(ofp,"%s\n",buf);
  334.                 while(fgets(buf,sizeof(buf),ifp))
  335.                     fputs(buf,ofp);
  336.             }
  337.             else
  338.                 store_keys(ofp);
  339.             break;    /* keys must be last thing in file */
  340.         }
  341.     }
  342. /* clean up */
  343.     fclose(ofp);
  344.     fclose(ifp);
  345.     if(rename(tmpfile,KEYFILE))
  346.     {
  347.         slip_message("Unable to replace the original setup file.");
  348.         wait_message();
  349.         return;
  350.     }
  351. }
  352.  
  353.